<template>
    <!-- 业态分类 -->
    <a-select v-bind="$attrs" v-on="$listeners" :placeholder="placeholder" v-model="currentValue" allowClear>
        <a-spin v-if="fetching" slot="notFoundContent" size="small" />
        <a-select-option :disabled="!i.enabled" v-else v-for="i in list" :key="i.id" :value="i.id">{{ i.name }}</a-select-option>
    </a-select>
</template>
<script>
import { getSelectListByType } from '@/api/businessAffairs/common.js'

export default {
    name: 'SelectFormat',
    inheritAttrs: false,
    props: {
        // v-model绑定的数据
        value: {
            type: [Array, String],
        },
        // 需要联动的表单名，
        form: {
            type: Object,
            default: function () {
                return {}
            },
        },
        // placeholder
        placeholder: {
            type: String,
            default: function () {
                return '请选择'
            },
        },
        // 是否获取全部列表（包括停用项）
        isAll: {
            type: Boolean,
            default() {
                return true
            },
        },
    },
    data() {
        return {
            fetching: false,
            list: [],
        }
    },
    mounted() {
        this.getList()
    },
    methods: {
        async getList() {
            this.fetching = true
            let params = {  type: 14,pageSize:1000 }
            const [err, res] = await getSelectListByType(params)
            this.fetching = false
            if (err) {
                this.$message.error('err:', res.message)
            } else {
                this.list = res.data
            }
        },
    },
    computed: {
        // 转换绑定的选中数据，与v-model同步
        currentValue: {
            get: function () {
                if (this.$listeners.getOption) {
                  let res = this.list?.find((item) => item.id == this.value)
                  this.$emit('getOption', res)
                }
                return this.value
            },
            set: function (value) {
                this.$emit('input', value)
            },
        },
    },
}
</script>
